package com.zyc.app.woa.utils;

import android.content.Context;
import android.content.SharedPreferences;
import com.zyc.app.woa.utils.Caches.CacheKey;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zyc on 12/28/14.
 */
public class SettingHelper {
    public static final String PREFERENCE_FILE = "woaSettings";
    private static SharedPreferences sPreferences;

    public static void init(Context context) {
        sPreferences = context.getSharedPreferences(PREFERENCE_FILE, Context.MODE_PRIVATE);
    }

    public static void put(CacheKey key, Object value) {
        if (value instanceof Boolean) {
            sPreferences.edit().putBoolean(key.name(), (Boolean) value);
        } else if (value instanceof Integer) {
            sPreferences.edit().putInt(key.name(), (Integer) value);
        } else if (value instanceof Long) {
            sPreferences.edit().putLong(key.name(), (Long) value);
        } else if (value instanceof String) {
            sPreferences.edit().putString(key.name(), (String) value);
        }
        sPreferences.edit().apply();
    }

    public static Object get(CacheKey key, Object defaultValue) {
        Object obj = null;
        if (defaultValue instanceof Boolean) {
            obj = sPreferences.getBoolean(key.name(), (Boolean) defaultValue);
        } else if (defaultValue instanceof Integer) {
            obj = sPreferences.getInt(key.name(), (Integer) defaultValue);
        } else if (defaultValue instanceof Long) {
            obj = sPreferences.getLong(key.name(), (Long) defaultValue);
        } else if (defaultValue instanceof String) {
            obj = sPreferences.getString(key.name(), (String) defaultValue);
        }

        return obj;
    }

    public static Map<String, ?> getAll() {
        return sPreferences.getAll();
    }

    public static boolean getBoolean(CacheKey key, boolean defaultValue) {
        return sPreferences.getBoolean(key.name(), defaultValue);
    }

}
